package com.remeberword;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by arch on 2018/3/12.
 */

public class AddWordActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_word);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout.setStatusBarBackground(R.color.colorPrimaryDark);
        NavigationView navView = (NavigationView) findViewById(R.id.nav_view);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
        }
        actionBar.setTitle("单词管理");
        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.show_word:
                        startActivity(new Intent(AddWordActivity.this, MainActivity.class));
                        break;
                    case R.id.freq_manager:
                        startActivity(new Intent(AddWordActivity.this, WordManageActivity.class));
                        break;
                    case R.id.backup:
                        startActivity(new Intent(AddWordActivity.this, BackupRestoreActivity.class));
                        break;
                }
                mDrawerLayout.closeDrawers();
                return true;
            }
        });

        final MyDatabaseHelper dbHelper = new MyDatabaseHelper(this, "WordPair.db", null);
        Button button = (Button) findViewById(R.id.submit);
        final EditText englishText = (EditText) findViewById(R.id.input_english);
        final EditText chineseText = (EditText) findViewById(R.id.input_chinese);
        englishText.requestFocus();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String english = englishText.getText().toString().trim();
                String chinese = chineseText.getText().toString().trim();
                if (english.length() == 0 || chinese.length() == 0) {
                    Toast.makeText(AddWordActivity.this, "不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                String sql = "select english, chinese from wordPair where english = ?";
                Cursor cursor = db.rawQuery(sql, new String[] {english});
//                Cursor cursor = db.query("wordPair", new String[] {"english"}, "english = ?", new String[] { english }, null, null, null);
                if (cursor.moveToFirst()) {
                    sql = "update wordPair set frequency = frequency + 1, chinese = ? where english = ?";
                    String chinese_store = cursor.getString(cursor.getColumnIndex("chinese"));
                    String temp;
                    if (chinese.equals(chinese_store))
                        temp = chinese_store;
                    else
                        temp = chinese_store + "；" + chinese;
                    db.execSQL(sql, new String[]{temp, english});
                    Toast.makeText(AddWordActivity.this, "词频已更新", Toast.LENGTH_LONG).show();
                } else {
                    ContentValues values = new ContentValues();
                    values.put("english", english);
                    values.put("chinese", chinese);
                    values.put("frequency", 1);
                    db.insert("wordPair", null, values);
                    Toast.makeText(AddWordActivity.this, "添加成功", Toast.LENGTH_LONG).show();
                }
                cursor.close();
                englishText.setText("");
                englishText.requestFocus();
                chineseText.setText("");
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                break;
        }
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("添加单词");
    }
}
